home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-10-29 | 2.9 KB | 163 lines |
- /*
- * File:
- * Bounce.java
- *
- * Copyright (c) 1996-1997 SuperCede, Inc. All rights reserved.
- *
- */
-
- import java.awt.*;
- import java.awt.image.*;
- import java.applet.*;
- import java.net.*;
-
- public class Bounce extends Applet implements Runnable
- {
- Thread worker = null;
- Callback helper = null;
-
- Graphics shadow_graphics = null;
- Image shadow_image = null;
-
- int x = 0;
- int y = 0;
-
- int x_direction = 1;
- int y_direction = 1;
-
- Dimension window_size = null;
-
- public void setBounds (int x, int y, int width, int height)
- {
- super.setBounds (x, y, width, height);
- window_size = getSize ();
- shadow_image = createImage (window_size.width, window_size.height);
- shadow_graphics = shadow_image.getGraphics ();
- prepareImage (shadow_image, this);
- }
-
- public void draw (Graphics graphics)
- {
- graphics.clearRect (0, 0, window_size.width, window_size.height);
- graphics.setColor (helper.getColor());
- int object_size = helper.getSize();
- switch (helper.getShape ())
- {
- case Callback.Rectangle:
- graphics.fillRect (x, y, object_size, object_size);
- break;
-
- case Callback.Circle:
- graphics.fillOval (x, y, object_size, object_size);
- break;
-
- default:
- break;
- }
- }
-
- public void paint (Graphics graphics)
- {
- draw (shadow_graphics);
- graphics.drawImage (shadow_image, 0, 0, null);
- }
-
- public void update (Graphics graphics)
- {
- paint (graphics);
- }
-
- public void start()
- {
- if (worker == null)
- {
- helper = new Callback ();
- worker = new Thread (this);
- worker.start ();
- }
- }
-
- public void stop()
- {
- if (worker != null)
- {
- worker.stop ();
- worker = null;
- helper = null;
- }
- }
-
- public void run ()
- {
- while (true)
- {
-
- // Advance the ball.
-
- int object_size = helper.getSize ();
- int step_size = helper.getStep ();
-
- x += x_direction * step_size;
- y += y_direction * step_size;
-
- if (x <= 0)
- {
- x_direction = Math.abs (x_direction);
- x = 0;
- }
-
- int x_limit = window_size.width - object_size;
- if (x >= x_limit)
- {
- x_direction = -Math.abs (x_direction);
- x = x_limit;
- }
-
- if (y <= 0)
- {
- y_direction = Math.abs (y_direction);
- y = 0;
- }
-
- int y_limit = window_size.height - object_size;
- if (y >= y_limit)
- {
- y_direction = -Math.abs (y_direction);
- y = y_limit;
- }
-
- // Paint the current image.
-
- repaint ();
-
- // Wait for some time to pass.
-
- try
- {
- Thread.sleep (helper.getSleep());
- }
- catch (InterruptedException e)
- {
- }
- }
- }
-
- public static void main (String params [])
- {
- Frame frame = new Application ();
- frame.setTitle ("Bounce");
- frame.setBounds (10, 10, 300, 400);
- frame.addNotify ();
-
- Applet applet = new Bounce ();
- frame.add ("Center", applet);
- applet.addNotify ();
- frame.doLayout ();
-
- applet.init ();
- applet.start ();
-
- frame.show ();
- }
- }
-